<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-09 10:07:06
 * @LastEditTime: 2019-10-17 14:13:05
 * @LastEditors: Please set LastEditors
 -->
<template>
  <div class="ctu animated bounceInRight">
    <div class="header">
      <div @click="histry">
        <img src="../../assets/histry.png">
        返回
      </div>
      <span class="reg" style="margin-left:-0.4rem;">切换企业</span>
      <span class="log"></span>
    </div>
    <div class="con" v-if="look">
        <p class="text1">请您选择企业（单选）</p>
        <ul>
            <li v-for="(i,index) in list" :key='index' @click="qiehuan(index)">
                <div><p :class="i.toggle_flag==1?'p2':'p1'"></p>{{i.ent_name}}</div>
                <span>已认证</span>
            </li>
        </ul>
        <div class="con_sure" @click="sure">确定</div>
    </div>
    <div class="con2" v-if="!look">
        <img src="../../assets/back_qiehuan.png" alt="">
        <p>管理员暂时还未添加任何认证企业</p>
    </div>
  </div>
</template>

<script>
import { check_login,query_user_ent_list,toggle_user_ent } from "../../api/api.js";
export default {
  name: "ctu",
  data() {
    return {
        phone:'',
        user_id:'',
        list:[],
        ent_id:'',
        look:true,
        qiye_xuanze_num:'',
        mlbb_xinxi:''
    };
  },
  mounted() {
    if (sessionStorage.getItem("mobile") != null) {
      this.phone = JSON.parse(sessionStorage.getItem("mobile"));
    }
    check_login({
      data: {
        mobile: this.phone
      }
    }).then(res => {
      if (res.data.code == 11001) {
      } else if (res.data.code == 11000) {
        this.$toast("未登录，请重新登录");
        this.$router.push("/");
      }
    });
    this.jin_qieqiye()
  },
  created(){
      if (sessionStorage.getItem("data_mobal") != null) {
        this.user_id = JSON.parse(
          JSON.parse(sessionStorage.getItem("data_mobal"))
        ).user_id;
      }
      if (sessionStorage.getItem("mlbb_xinxi") != null) {
        this.mlbb_xinxi =JSON.parse(sessionStorage.getItem("mlbb_xinxi"));
      }
  },
  watch: {},
  methods: {
    // 返回上一层
    histry() {
      this.$router.push('/mine');
    },
    // 企业列表
    jin_qieqiye(){
      this.$loading.open();
      query_user_ent_list({data:{
        user_id:this.user_id
      }}).then(res=>{
        this.$loading.close();
        console.log(JSON.parse(res.data.body.data))
        console.log(res)
        if(res.data.code==10080){
        //   this.$router.push('/ctu')
        // 更改   说不上啥   总觉得不对   也许之前是为了对比toggle_flag
          // for(var i=0;i<JSON.parse(res.data.body.data).length;i++){
          //     if(JSON.parse(res.data.body.data)[i].toggle_flag==1){
          //         this.ent_id=JSON.parse(res.data.body.data)[i].ent_id
          //     }
          // }
          // 20191217更改
          let date=JSON.parse(res.data.body.data)
          console.log(date)
          // for(let i=0;i<date.length;i++){
          //   if(this.mlbb_xinxi.ent_name==date[i].ent_name){
          //     this.qiye_xuanze_num=i+''
          //   }
          // }
          if(date.length!=0){
              this.list=date
              this.look=true
          }else{
              this.look=false
          }
        }else if(res.data.code==10081){
          this.$toast('获取失败')
        }
      })
    },
    // 列表切换
    qiehuan(index){
      // this.qiye_xuanze_num=index+''
      // this.ent_id=this.list[index].ent_id
        this.list[index].toggle_flag=1
        this.ent_id=this.list[index].ent_id
        for(let i=0;i<this.list.length;i++){
            if(i!=index){
                this.list[i].toggle_flag=0
            }
        }
    },
    // 切换按钮
    sure(){
        if(this.ent_id==''){
            this.ent_id=this.mlbb_xinxi.ent_id
        }
        // console.log(this.list)
        this.$loading.open();
        toggle_user_ent({data:{
            ent_id:this.ent_id,
            user_id:this.user_id
        }}).then(res=>{
            this.$loading.close();
            console.log(res)
            if(res.data.code==10082){
                this.$toast('切换成功')
                // sessionStorage.setItem("mlbb_xinxi", JSON.stringify(this.date.staff_info));
                this.$router.push('/mine')
            }else if(res.data.code==10083){
                this.$toast('切换失败')
            }
        })
        
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.ctu {
  height: 100vh;
  background: #fff;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 0.5rem;
    line-height: 0.5rem;
    padding: 0 0.15rem;
    border-bottom: 0.01rem solid #eee;
    div {
      display: flex;
      justify-content: left;
      align-items: center;
      color: #979797;
      font-size: 0.14rem;
      img {
        height: 0.16rem;
        width: 0.08rem;
        margin-right: 0.08rem;
      }
    }
    .reg {
      font-size: 0.18rem;
      color: #1a2533;
      font-weight: 500;
    }
    .log {
      font-size: 0.14rem;
      color: #e82716;
      font-weight: bold;
    }
  }
  .con{
      padding: 0 0.15rem;
      .text1{
        font-size: 0.14rem;
        color:#1A2533;
        margin:0.1rem 0;
    }
    ul{
        li{
            height:0.33rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            div{
                color:#888888;
                font-size: 0.14rem;
                display: flex;
                justify-content: left;
                align-items: center;
                p{
                    height:0.14rem;
                    width:0.14rem;
                    border-radius: 50%;
                    margin-right: 0.1rem;
                }
                .p1{
                    border:0.01rem solid #C8C3C3;
                    background: #fff;
                }
                .p2{
                    border:0.01rem solid #A5C6FD;
                    background: #A5C6FD;
                }
            }
            span{
                color:#6ABFA7;
                font-size: 0.14rem;
            }
        }
    }
    .con_sure{
        height:0.47rem;
        width: 100%;
        margin-top:0.7rem;
        text-align: center;
        color:#fff;
        line-height: 0.47rem;
        background:linear-gradient(270deg,rgba(252,101,101,1) 0%,rgba(232,39,22,1) 100%);
        border-radius:0.02rem;
    }
  }
  .con2{
      img{
          height:1.62rem;
          width:2.38rem;
          display: block;
          margin: 0 auto;
          margin-top: 0.76rem;
      }
      p{
          font-size: 0.14rem;
          color:#888888;
          margin-top: 0.32rem;
          text-align: center;
      }
  }
  
}
</style>